/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.common;

import com.arg.deploy.bean.Tag;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author sorasaks
 */
public class GenerateScriptUtil {

    private static final Logger LOG = Logger.getLogger(GenerateScriptUtil.class);
    public static final String IP_CVS_SERVER_PATH = "172.21.44.206/usr/local/arg";
    public static final String CVS_USER = "sorasaks";

    public static void generate(List<Tag> object, String tagName, String cvsName, String outputFile) {
        StringBuilder script = new StringBuilder();
        for (Tag o : object) {
            String scp = null;
            if (o.getMode() == null || o.getMode().trim().equals("MODIFY")) {
                if (o.getVersion().trim().equalsIgnoreCase("New File")) {
                    scp = "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " co " + o.getPath() + "\n";
                } else {
                    scp = "";
                }
                scp += "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " tag -r " + o.getRevision() + " -F " + tagName + " " + o.getPath() + "\n";
                LOG.info("::::::::::::::::::::: script = " + scp);
            } else if (o.getMode() == null || o.getMode().trim().equals("DEL")) {
                scp = "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " tag -d " + tagName + " " + o.getPath() + "\n";
                LOG.info("::::::::::::::::::::: script = " + scp);
            } else if (o.getMode() == null || o.getMode().trim().equals("ADD")) {
                scp = "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " co " + o.getPath() + "\n";
                scp += "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " tag -r " + tagName + " " + o.getPath() + "\n";
                LOG.info("::::::::::::::::::::: script = " + scp);
            }
            script.append(scp);
        }

        FileUtil.createFile(outputFile, script);
    }

    public static void generateRollback(List<Tag> object, String tagName, String cvsName, String outputFile) {
        StringBuilder script = new StringBuilder();
        for (Tag o : object) {
            if (!o.getVersion().equals("New File")) {
                String scp = "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " tag -r " + o.getVersion() + " -F " + tagName + " " + o.getPath() + "\n";
                script.append(scp);
            } else {
                String scp = "cvs -d :pserver:" + cvsName + "@" + IP_CVS_SERVER_PATH + " tag -d " + tagName + " " + o.getPath() + "\n";
                script.append(scp);
            }
        }

        FileUtil.createFile(outputFile, script);
    }
}
